import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Message } from './message.entity';

@Injectable()
export class MessageService {
  constructor(
    @InjectRepository(Message)
    private messageRepository: Repository<Message>,
  ) {}

  async getMessagesByConversationId(
    conversationId: number,
  ): Promise<Message[]> {
    return this.messageRepository.find({
      where: { conversationId },
      order: { sentAt: 'ASC' },
    });
  }

  async createMessage(
    conversationId: number,
    senderId: number,
    content: string,
    messageType: string = 'text',
  ): Promise<Message> {
    const message = this.messageRepository.create({
      conversationId,
      senderId,
      content,
      messageType,
    });

    return this.messageRepository.save(message);
  }
}
